<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;

class LiveResourceVideoController extends AdminbaseController{
	private $live_resource_video;
	
	function _initialize() {
		parent::_initialize();
		$this->live_resource_video = D("Common/LiveResourceVideo");
	}
	
	function video_list(){
		$live_id = I("get.live_id");
		
		//查询视频资源
		$videoList = $this->live_resource_video->get_list($live_id);
		
		if(!$videoList || empty($videoList)){
			$videoList = array();
		}
		$videoList[] = array('video_id'=>0, 'video_name'=>'','file_name'=>'','category'=>1,'input_file_id'=>'');
		$this->assign('videoList', $videoList);
		$this->display();
	}
	
	function save_video(){
		$live_id = I("get.live_id");
		$video_id = I("post.video_id");
		$category = I("post.category");
		$input_file_id = I("post.input_file_id");
		$video_name = I("post.video_name");
		if(0 == $video_id){
			//新增
			if($input_file_id && !empty($input_file_id)){
				$data = $this->live_resource_video->get_video($input_file_id, $category == 2 ? 1 : 0);
				if(0 == $data){
					return $this->error("参数错误！");
				}else if(-1 == $data){
					return $this->error("调用腾讯云接口失败！");
				}else if(-2 == $data){
					return $this->error("腾讯云fileId不存在！");
				}else{
					$video_id = $this->create_id();
					$data['video_id'] = $video_id;
					$data['live_id'] = $live_id;
					$data['input_file_id'] = $input_file_id;
					$data['video_name'] = $video_name;
					$data['category'] = $category;
					$data['status'] = 2;
					$this->live_resource_video->add($data);
					return $this->success("保存成功！");
				}
			}else{
				$video_id = $this->create_id();
				$file_id = $this->live_resource_video->uploadVideo($video_id, $category == 2 ? 1 : 0);
				if(false == $file_id){
					return $this->error("视频文件上传失败！");
				}else{
					$data = array(
							'video_id'=>$video_id,
							'live_id'=>$live_id,
							'category'=>$category,
							'file_name'=>$_FILES['file']['name'],
							'video_name'=>$video_name,
							'file_id' => $file_id
					);
					$this->live_resource_video->add($data);
					return $this->success("保存成功！");
				}
			}
		}else{
			//修改
			$video = $this->live_resource_video->field('file_id,input_file_id')->find($video_id);
			if($input_file_id && !empty($input_file_id) && $video['input_file_id'] != $input_file_id){
				$data = $this->live_resource_video->get_video($input_file_id,$category == 2 ? 1 : 0);
				if(0 == $data){
					return $this->error("参数错误！");
				}else if(-1 == $data){
					return $this->error("调用腾讯云接口失败！");
				}else if(-2 == $data){
					return $this->error("腾讯云fileId不存在！");
				}else{
					$data['input_file_id'] = $input_file_id;
					$data['video_name'] = $video_name;
					$data['category'] = $category;
					$data['status'] = 2;
					$this->live_resource_video->where(array('video_id'=>$video_id))->save($data);
				}
			}else{
				//$file_id = $this->live_resource_video->uploadVideo($video_id);
				$data = array(
						'category'=>$category,
						'video_name'=>$video_name
				);
				/* if(false != $file_id){
					$data['file_id'] = $file_id;
					$data['file_name'] = $_FILES['file']['name'];
				} */
				$this->live_resource_video->where(array('video_id'=>$video_id))->save($data);
			}
			return $this->success("保存成功！");
		}
		
	}
	
	private function create_id(){
		return date('Y') . time() . rand(0000, 9999);
	}
	
	function delete_video(){
		$video_id = I("post.video_id");
		$result = $this->live_resource_video->where(array('video_id'=>$video_id))->save(array('status'=>0));
		if($result){
			$this->success("删除成功！");
		}else{
			return $this->error("删除失败！");
		}
	}
	
}